/**
 * 项目名称（软件名称）：忠旺机械物资管理系统(mms)
 * 研发设计单位：忠旺机械设备制造有限公司机械设计院智能控制设计科设计与研发。
 * 版权所有：忠旺机械设备制造有限公司
 * 注：本项目的一切技术专利，使用规范归解释权由忠旺机械设备制造有限公司所有。
 * 软件版本：Ver 1.0.0
 * 文件路径：com.zhongwang.mms.module.purchase.service
 * 创建时间：2020年05月04日 11时19分
 **/
package com.zhongwang.mms.module.purchase.service;

import com.zhongwang.mms.base.BaseService;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.*;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.purchase.constant.PurchaseConstant;
import com.zhongwang.mms.module.purchase.dao.*;
import com.zhongwang.mms.module.purchase.model.PInventoryModel;
import com.zhongwang.mms.module.purchase.model.VWProject;
import com.zhongwang.mms.module.purchase.model.adjust.CompareAdjust;
import com.zhongwang.mms.module.purchase.model.adjust.PAdjustModel;
import com.zhongwang.mms.module.purchase.model.check.PAdjustmentApplyModel;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName： PAdjustService
 * @Description： 调整业务逻辑层
 * @Author： 徐一贺
 * @Date： 2020/5/4 11:19
 * @Version： V1.0
 **/
@Service
@Log4j2
public class PurAdjustService extends BaseService {
    @Autowired
    private PurAdjustDao purAdjustDao;

    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;

    @Autowired
    private PurPInventoryDao purPInventoryDao;

    @Autowired
    private PurWarInManageService purWarInManageService;

    @Autowired
    private PurPOrderDao purPOrderDao;

    @Autowired
    private PurPOrderDetailDao purPOrderDetailDao;

    @Autowired
    private PurCheckDao purCheckDao;

    /**
     * @FunctionName: generateAdjCode
     * @Description: 生成批次码
     * @Author: 徐一贺
     * @CreateDate: 2020/5/4 11:14
     * @Phone: 18241927380
     * @Return: [code] 批次码
     * @Version: 1.0.0
     */
    public String generateAdjCode() {
        //获取最后的批次码
        String lastCode = purAdjustDao.getLastCode("PADJ");
        String code = "";
        //获取当前时间简码
        String datetimeCode = new SimpleDateFormat("yyMMdd").format(new Date());
        Integer sn = 0;

        //如果批次码不存在，则生成新的码从000001开始
        if (null == lastCode || lastCode.trim().equals("")) {
            code = "PADJ" + new SimpleDateFormat("yyMMdd").format(new Date()) + "000001";
        } else if (!lastCode.substring(4, 10).equals(datetimeCode)) {
            code = "PADJ" + new SimpleDateFormat("yyMMdd").format(new Date()) + "000001";
        }
        //如果是批次码存在，则按照顺序生成新的批次码
        else {
            if (lastCode.length() > 0) {
                //获取外购件库最新最后的入库单号
                sn = Integer.parseInt(lastCode.substring(10));
            }
            code = "PADJ" + new SimpleDateFormat("yyMMdd").format(new Date()) + String.format("%06d", (sn + 1));
        }
        return code;
    }

    /**
     * @FunctionName: findApplyList
     * @Description: 获取库存调整申请列表
     * @Author: 徐一贺
     * @CreateDate: 2020/5/4 18:09
     * @Phone: 18241927380
     * @Param: [param] 检索条件
     * @Return: com.zhongwang.mms.module.purchase.model.adjust.PAdjustModel 列表数据
     * @Version: 1.0.0
     */
    public List<PAdjustModel> findApplyList(PAdjustModel param) {
        if (param.getEndDate() != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(param.getEndDate());
            cal.add(Calendar.DAY_OF_YEAR, 1);
            param.setEndDate(cal.getTime());
        }
        return purAdjustDao.findApplyList(param);
    }

    /**
     * @FunctionName: findReviewList
     * @Description: 获取库存调整审核列表
     * @Author: 徐一贺
     * @CreateDate: 2020/5/6 8:53
     * @Phone: 18241927380
     * @Param: [param] 检索条件
     * @Return: com.zhongwang.mms.module.purchase.model.adjust.PAdjustModel 列表数据
     * @Version: 1.0.0
     */
    public List<PAdjustModel> findReviewList(PAdjustModel param) {
        if (param.getEndDate() != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(param.getEndDate());
            cal.add(Calendar.DAY_OF_YEAR, 1);
            param.setEndDate(cal.getTime());
        }
        return purAdjustDao.findReviewList(param);
    }

    /**
     * @FunctionName: findWarehouseInfoList
     * @Description: 获得仓库信息列表
     * @Author: 徐一贺
     * @CreateDate: 2020/5/4 19:51
     * @Phone: 18241927380
     * @Param:
     * @Return: [List<TWarehouseInfo>] 仓库信息列表
     * @Version: 1.0.0
     */
    public List<TWarehouseInfo> findWarehouseInfoList() {
        return tWarehouseInfoDao.findWarehouseInfoListByWhiType("");
    }

    /**
     * @FunctionName: applyRemove
     * @Description: 跟库主键删除（逻辑）调整申请
     * @Author: 徐一贺
     * @CreateDate: 2020/5/4 20:38
     * @Phone: 18241927380
     * @Param: [param]
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult applyRemove(PAdjustModel param) {
        JsonResult jsonResult = new JsonResult();
        try {
            String[] adjIdArr = param.getAdjIds().split(",");
            for (String adjIdStr : adjIdArr) {
                PAdjustModel pAdjustModel = purAdjustDao.getPAdjustModelByPrimaryKey(Integer.valueOf(adjIdStr));
                logService.saveLog("【外购件删除库存调整申请】", "删除的申请物料名称是:" + pAdjustModel.getAdjMatDesc());
            }
            purAdjustDao.updatePAdjustByAdjPIds(param);
            purAdjustDao.updatePAdjustByPrimaryKeys(param);

            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("删除失败！");
        }
        return jsonResult;
    }

    /**
     * @FunctionName: applyInvList
     * @Description: 获取库存列表
     * @Author: 徐一贺
     * @CreateDate: 2020/5/5 8:12
     * @Phone: 18241927380
     * @Param: [param] 查询条件
     * @Return: com.zhongwang.mms.common.JsonResult 库存列表
     * @Version: 1.0.0
     */
    public List<PInventoryModel> findPInventoryModelList(PInventoryModel param) {
        return purPInventoryDao.findInventoryListByAdjust(param);
    }

    /**
     * @FunctionName: applyGetInventory
     * @Description: 根据主键获取库存信息
     * @Author: 徐一贺
     * @CreateDate: 2020/5/5 8:48
     * @Phone: 18241927380
     * @Param: [param.ityId] 主键
     * @Return: [com.zhongwang.mms.common.JsonResult.data] 库存信息
     * @Version: 1.0.0
     */
    public PInventoryModel getInventoryByPrimaryKey(Integer ityId) {
        return purPInventoryDao.getInventoryByPrimaryKey(ityId);
    }

    /**
     * @FunctionName: applyCreate
     * @Description: 创建调整申请
     * @Author: 徐一贺
     * @CreateDate: 2020/5/5 9:47
     * @Phone: 18241927380
     * @Param: [param] 调整申请
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult applyCreate(PAdjustModel param) {
        JsonResult jsonResult = new JsonResult();
        try {
            String adjCode = generateAdjCode();
            //如果不是新增
            if (null != param.getItyId()) {
                //获取原库存信息
                PInventoryModel sourcePAdjustPinventoryModel = purPInventoryDao.getInventoryByPrimaryKey(param.getItyId());
                PInventoryModel pInventoryModel = purPInventoryDao.getInventoryByPrimaryKey(sourcePAdjustPinventoryModel.getItyId());
                //如果物料描述或单价不相同生成两条数据
                if (!sourcePAdjustPinventoryModel.getItyMatDesc().equals(param.getItyMatDesc()) ||
                        !sourcePAdjustPinventoryModel.getItyNumUnit().equals(param.getItyNumUnit()) ||
                        !sourcePAdjustPinventoryModel.getItyPrice().equals(param.getItyPrice())
                        || !sourcePAdjustPinventoryModel.getItyNum().equals(param.getItyNum())
                ) {
                    PAdjustModel beforePAdjustModel = new PAdjustModel();
                    beforePAdjustModel.setAdjItyId(sourcePAdjustPinventoryModel.getItyId());
                    beforePAdjustModel.setAdjMatDesc(sourcePAdjustPinventoryModel.getItyMatDesc());
                    beforePAdjustModel.setAdjMatCode(sourcePAdjustPinventoryModel.getItySapCode());
                    beforePAdjustModel.setAdjNum(sourcePAdjustPinventoryModel.getItyNum());
                    beforePAdjustModel.setAdjUnit(sourcePAdjustPinventoryModel.getItyNumUnit());
                    beforePAdjustModel.setAdjPrice(sourcePAdjustPinventoryModel.getItyPrice());
                    beforePAdjustModel.setAdjAmount(sourcePAdjustPinventoryModel.getItyAmount());
                    beforePAdjustModel.setAdjTrackCode(sourcePAdjustPinventoryModel.getItyTrackCode());
                    beforePAdjustModel.setAdjProCode(sourcePAdjustPinventoryModel.getItyProCode());
                    beforePAdjustModel.setAdjProName(sourcePAdjustPinventoryModel.getItyProName());
                    beforePAdjustModel.setAdjWhiId(sourcePAdjustPinventoryModel.getItyWhiId());
                    beforePAdjustModel.setAdjPosition(sourcePAdjustPinventoryModel.getItyPosition());
                    beforePAdjustModel.setAdjBookkeepingDate(sourcePAdjustPinventoryModel.getItyBookkeepingDate());
                    beforePAdjustModel.setAdjSpec(sourcePAdjustPinventoryModel.getItyRemark());
                    beforePAdjustModel.setAdjReason(param.getAdjReason());
                    beforePAdjustModel.setAdjStockType(param.getItyStockType());
                    beforePAdjustModel.setAdjCode(adjCode);
                    beforePAdjustModel.setAdjAppId(ShiroUtils.getUser().getId());
                    beforePAdjustModel.setAdjAppDatetime(new Date());
                    beforePAdjustModel.setAdjType(1);
                    beforePAdjustModel.setAdjNewFlag(0);
                    beforePAdjustModel.setAdjStatus(1);
                    beforePAdjustModel.setAdjIdentity(1);
                    beforePAdjustModel.setAdjDValue(0 - pInventoryModel.getItyNum());
                    if (!sourcePAdjustPinventoryModel.getItyMatDesc().equals(param.getItyMatDesc())) {
                        beforePAdjustModel.setAdjModel("desc");
                    }
                    if (!sourcePAdjustPinventoryModel.getItyNumUnit().equals(param.getItyNumUnit())) {
                        beforePAdjustModel.setAdjModel("unit");
                    }
                    if (!sourcePAdjustPinventoryModel.getItyPrice().equals(param.getItyPrice())) {
                        beforePAdjustModel.setAdjModel("price");
                    }
//                    if (!sourcePAdjustPinventoryModel.getItyNum().equals(param.getItyNum())) {
//                        beforePAdjustModel.setAdjModel(null);
//                    }
                    beforePAdjustModel.setAdjRemarkType(sourcePAdjustPinventoryModel.getItyRemarkType());
                    purAdjustDao.insertSelective(beforePAdjustModel);
                    PAdjustModel afterPAdjustModel = new PAdjustModel();
                    afterPAdjustModel.setAdjItyId(sourcePAdjustPinventoryModel.getItyId());
                    afterPAdjustModel.setAdjMatDesc(param.getItyMatDesc());
                    afterPAdjustModel.setAdjMatCode(sourcePAdjustPinventoryModel.getItySapCode());
                    afterPAdjustModel.setAdjNum(param.getItyNum());
                    afterPAdjustModel.setAdjUnit(param.getItyNumUnit());
                    afterPAdjustModel.setAdjPrice(param.getItyPrice());
                    afterPAdjustModel.setAdjAmount(sourcePAdjustPinventoryModel.getItyAmount());
                    afterPAdjustModel.setAdjTrackCode(sourcePAdjustPinventoryModel.getItyTrackCode());
                    afterPAdjustModel.setAdjProCode(sourcePAdjustPinventoryModel.getItyProCode());
                    afterPAdjustModel.setAdjProName(sourcePAdjustPinventoryModel.getItyProName());
                    afterPAdjustModel.setAdjWhiId(sourcePAdjustPinventoryModel.getItyWhiId());
                    afterPAdjustModel.setAdjPosition(sourcePAdjustPinventoryModel.getItyPosition());
                    afterPAdjustModel.setAdjBookkeepingDate(sourcePAdjustPinventoryModel.getItyBookkeepingDate());
                    afterPAdjustModel.setAdjSpec(sourcePAdjustPinventoryModel.getItyRemark());
                    afterPAdjustModel.setAdjReason(param.getAdjReason());
                    afterPAdjustModel.setAdjStockType(param.getItyStockType());
                    afterPAdjustModel.setAdjCode(adjCode);
                    afterPAdjustModel.setAdjAppId(ShiroUtils.getUser().getId());
                    afterPAdjustModel.setAdjAppDatetime(beforePAdjustModel.getAdjAppDatetime());
                    afterPAdjustModel.setAdjType(1);
                    afterPAdjustModel.setAdjNewFlag(0);
                    afterPAdjustModel.setAdjStatus(1);
                    afterPAdjustModel.setAdjPid(beforePAdjustModel.getAdjId());
                    afterPAdjustModel.setAdjIdentity(2);
                    afterPAdjustModel.setAdjDValue(sourcePAdjustPinventoryModel.getItyNum());
                    if (!sourcePAdjustPinventoryModel.getItyMatDesc().equals(param.getItyMatDesc())) {
                        afterPAdjustModel.setAdjModel("desc");
                    }
                    if (!sourcePAdjustPinventoryModel.getItyNumUnit().equals(param.getItyNumUnit())) {
                        afterPAdjustModel.setAdjModel("unit");
                    }
                    if (!sourcePAdjustPinventoryModel.getItyPrice().equals(param.getItyPrice())) {
                        afterPAdjustModel.setAdjModel("price");
                    }
                    if ((!sourcePAdjustPinventoryModel.getItyMatDesc().equals(param.getItyMatDesc()) && !sourcePAdjustPinventoryModel.getItyNum().equals(param.getItyNum()))
                            || (!sourcePAdjustPinventoryModel.getItyNumUnit().equals(param.getItyNumUnit()) && !sourcePAdjustPinventoryModel.getItyNum().equals(param.getItyNum()))
                            || (!sourcePAdjustPinventoryModel.getItyPrice().equals(param.getItyPrice()) && !sourcePAdjustPinventoryModel.getItyNum().equals(param.getItyNum()))) {
                        afterPAdjustModel.setAdjDValue(param.getItyNum());
                    } else if (!sourcePAdjustPinventoryModel.getItyNum().equals(param.getItyNum())) {
                        afterPAdjustModel.setAdjModel("num");
                        afterPAdjustModel.setAdjDValue(param.getItyNum() - sourcePAdjustPinventoryModel.getItyNum());
                    }
                    afterPAdjustModel.setAdjRemarkType(sourcePAdjustPinventoryModel.getItyRemarkType());
                    purAdjustDao.insertSelective(afterPAdjustModel);
                }
//                //如果数量不同生成一条数据
//                else if (!sourcePAdjustPinventoryModel.getItyNum().equals(param.getItyNum())) {
//                    PAdjustModel afterPAdjustModel = new PAdjustModel();
//
//                    afterPAdjustModel.setAdjItyId(sourcePAdjustPinventoryModel.getItyId());
//                    afterPAdjustModel.setAdjMatDesc(sourcePAdjustPinventoryModel.getItyMatDesc());
//                    afterPAdjustModel.setAdjMatCode(sourcePAdjustPinventoryModel.getItyMatCode());
//                    afterPAdjustModel.setAdjNum(param.getItyNum());
//                    afterPAdjustModel.setAdjUnit(sourcePAdjustPinventoryModel.getItyNumUnit());
//                    afterPAdjustModel.setAdjPrice(sourcePAdjustPinventoryModel.getItyPrice());
//                    afterPAdjustModel.setAdjAmount(sourcePAdjustPinventoryModel.getItyAmount());
//                    afterPAdjustModel.setAdjTrackCode(sourcePAdjustPinventoryModel.getItyTrackCode());
//                    afterPAdjustModel.setAdjProCode(sourcePAdjustPinventoryModel.getItyProCode());
//                    afterPAdjustModel.setAdjProName(sourcePAdjustPinventoryModel.getItyProName());
//                    afterPAdjustModel.setAdjWhiId(sourcePAdjustPinventoryModel.getItyWhiId());
//                    afterPAdjustModel.setAdjPosition(sourcePAdjustPinventoryModel.getItyPosition());
//                    afterPAdjustModel.setAdjBookkeepingDate(sourcePAdjustPinventoryModel.getItyBookkeepingDate());
//                    afterPAdjustModel.setAdjSpec(sourcePAdjustPinventoryModel.getItyRemark());
//                    afterPAdjustModel.setAdjReason(param.getAdjReason());
//                    afterPAdjustModel.setAdjStockType(sourcePAdjustPinventoryModel.getItyStockType());
//                    afterPAdjustModel.setAdjCode(adjCode);
//                    afterPAdjustModel.setAdjAppId(ShiroUtils.getUser().getId());
//                    afterPAdjustModel.setAdjAppDatetime(new Date());
//                    afterPAdjustModel.setAdjType(1);
//                    afterPAdjustModel.setAdjNewFlag(0);
//                    afterPAdjustModel.setAdjStatus(1);
//                    afterPAdjustModel.setAdjIdentity(2);
//                    afterPAdjustModel.setAdjDValue(pInventoryModel.getItyNum() - afterPAdjustModel.getAdjNum());
//                    purAdjustDao.insertSelective(afterPAdjustModel);
//                }
                else {
                    jsonResult.setType(JsonResult.ResultType.ERROR);
                    jsonResult.setMsg("您没有修改物料描述、单位、单价或数量，无法调整！");
                    return jsonResult;
                }
            }
            //如果是新增
            else {
                param.setAdjCode(adjCode);
                param.setAdjType(1);
                param.setAdjNewFlag(1);
                param.setAdjStatus(1);
                param.setAdjIdentity(2);
                param.setAdjAppId(ShiroUtils.getUser().getId());
                param.setAdjAppDatetime(new Date());
                param.setAdjDValue(param.getAdjNum());
                param.setAdjModel("num");
                purAdjustDao.insertSelective(param);
            }
            logService.saveLog("【外购件】创建库存调整成功！", "库存调整批次编号：" + adjCode);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("创建库存调整申请成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("创建库存调整申请失败！");
        }
        return jsonResult;
    }

    /**
     * @FunctionName: applyGetAdjust
     * @Description: 根据主键获取申请
     * @Author: 徐一贺
     * @CreateDate: 2020/5/5 9:47
     * @Phone: 18241927380
     * @Param: [param] 主键
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: 1.0.0
     */
    public PAdjustModel applyGetAdjust(PAdjustModel param) {
        return purAdjustDao.getPAdjustModelByPrimaryKey(param.getAdjId());
    }

    /**
     * @FunctionName: applySaveAdjust
     * @Description: 保存调整申请
     * @Author: 徐一贺
     * @CreateDate: 2020/5/5 15:02
     * @Phone: 18241927380
     * @Param: [param] 主键
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult applySaveAdjust(PAdjustModel param) {
        JsonResult jsonResult = new JsonResult();
        try {
            PAdjustModel beforeLogModel = purAdjustDao.getPAdjustModelByPrimaryKey(param.getAdjId());
            PAdjustModel updateModel = new PAdjustModel();
            updateModel.setAdjId(param.getAdjId());
            updateModel.setAdjNum(param.getAdjNum());
            updateModel.setAdjReason(param.getAdjReason());
            purAdjustDao.updateSelectiveByPrimaryKey(updateModel);
            PAdjustModel afterLogModel = purAdjustDao.getPAdjustModelByPrimaryKey(param.getAdjId());
            logService.saveLog("【外购件】编辑调整申请", "编辑前的物料描述：【" + beforeLogModel.getAdjMatDesc() +
                    "】,编辑前的数量：【" + beforeLogModel.getAdjNum() +
                    "】,编辑前的调整理由：【" + beforeLogModel.getAdjReason() + "】<br>" +
                    "编辑后的物料描述：【" + afterLogModel.getAdjMatDesc() +
                    "】,编辑后的数量：【" + afterLogModel.getAdjNum() +
                    "】,编辑后的调整理由：【" + afterLogModel.getAdjReason() + "】");
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("编辑库存调整申请成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("编辑库存调整申请失败！");
        }
        return jsonResult;
    }

    /**
     * @FunctionName: codeExist
     * @Description: 验证项目号是否存在
     * @Author: 徐一贺
     * @CreateDate: 2020/5/5 16:08
     * @Phone: 18241927380
     * @Param: [param]
     * @Return: java.lang.String
     * @Version: 1.0.0
     */
    public Boolean codeExist(VWProject param) {
        VWProject vwProject = purAdjustDao.findVWProjectByCode(param);
        return vwProject != null;
    }

    /**
     * @FunctionName: applySubmit
     * @Description: 调整申请提交
     * @Author: 徐一贺
     * @CreateDate: 2020/5/5 16:08
     * @Phone: 18241927380
     * @Param: [param.ajdIds] 提交的主键们
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult applySubmit(PAdjustModel param) {
        JsonResult jsonResult = new JsonResult();
        try {
            List<PAdjustModel> pAdjustModelList = purAdjustDao.findApplyListByAdjIds(param.getAdjIds());
            param.setAdjStatus(2);
            purAdjustDao.updatePAdjustByPrimaryKeys(param);
            purAdjustDao.updatePAdjustByAdjPIds(param);
            for (PAdjustModel pAdjustModel : pAdjustModelList) {
                logService.saveLog("【外购件】调整申请提交", "提交的调整申请物料描述：" + pAdjustModel.getAdjMatDesc());
            }
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("提交失败！");
        }
        return jsonResult;
    }

    /**
     * @FunctionName: applyGetCompareAdjust
     * @Description: 根据主键和父级依赖获取比较显示实体类
     * @Author: 徐一贺
     * @CreateDate: 2020/5/5 16:31
     * @Phone: 18241927380
     * @Param: [param] 主键 父级
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: 1.0.0
     */
    public CompareAdjust applyGetCompareAdjust(PAdjustModel param) {
        CompareAdjust compareAdjust = new CompareAdjust();
        compareAdjust.setBeforeAdjust(purAdjustDao.getPAdjustModelByPrimaryKey(param.getAdjPid()));
        compareAdjust.setAfterAdjust(purAdjustDao.getPAdjustModelByPrimaryKey(param.getAdjId()));
        return compareAdjust;
    }

    /**
     * @FunctionName: reviewAdopt
     * @Description: 审核通过
     * @DesignIdeas： 1.首先验证调整类别，如果是新增调整或盘点新增调整则直接生成库存数据和其相关的出入库明细数据。
     * 2.如果是调整申请和盘点调整，如果是盘点状态是盘亏，则先验证库存是否被标记（可用数量与在库数量书否相等），
     * 然后验证库存在库数量和可用数量是否足够调整，不足则提示。足够的话进行调整直接生成库存数据和其相关的出入
     * 库明细数据。
     * 3.如果是盘点状态是盘盈，则进行调整直接生成库存数据和其相关的出入库明细数据。
     * @Author: 徐一贺
     * @CreateDate: 2020/5/6 10:37
     * @Phone: 18241927380
     * @Param: [param] 主键
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult reviewAdopt(PAdjustModel param) {
        JsonResult jsonResult = new JsonResult();
        PAdjustModel pAdjustModel = purAdjustDao.getPAdjustModelByPrimaryKey(param.getAdjId());
        //当前状态是调整新增和盘点新增
        if (pAdjustModel.getAdjNewFlag().equals(1)) {
            POrder pOrder = new POrder();
            pOrder.setOdrCode(purWarInManageService.getOrderCode(PurchaseConstant.PRK));
            pOrder.setOdrType(1);
            pOrder.setOdrRedFlag(0);
            pOrder.setOdrChangeFlag(0);
            pOrder.setOdrReqFlag(0);
            pOrder.setOdrDate(new Date());
            pOrder.setOdrRemark("调整入库单");
            pOrder.setOdrMakeDate(new Date());
            pOrder.setOdrMakePersonId(ShiroUtils.getUser().getId());
            pOrder.setOdrAdjustFlag(1);
            purPOrderDao.insertSelective(pOrder);
            POrderDetail pOrderDetail = new POrderDetail();
            pOrderDetail.setOdaOdrId(pOrder.getOdrId());
            pOrderDetail.setOdaType(1);
            pOrderDetail.setOdaProCode(pAdjustModel.getAdjProCode());
            pOrderDetail.setOdaProName(pAdjustModel.getAdjProName());
            pOrderDetail.setOdaMatDesc(pAdjustModel.getAdjMatDesc());
            pOrderDetail.setOdaNum(pAdjustModel.getAdjNum());
            pOrderDetail.setOdaNumPrice(pAdjustModel.getAdjPrice());
            pOrderDetail.setOdaPosition(pAdjustModel.getAdjPosition());
            pOrderDetail.setOdaWhiId(pAdjustModel.getAdjWhiId());
            pOrderDetail.setOdaNumUnit(pAdjustModel.getAdjUnit());
            pOrderDetail.setOdaTotalPrice(pAdjustModel.getAdjAmount());
            pOrderDetail.setOdaSpec(pAdjustModel.getAdjSpec());
            pOrderDetail.setOdaBookkeepingDate(pAdjustModel.getAdjBookkeepingDate());
            pOrderDetail.setOdaSapCode(pAdjustModel.getAdjMatCode());
            pOrderDetail.setOdaTrackCode(pAdjustModel.getAdjTrackCode());
            pOrderDetail.setOdaRedFlag(0);
            pOrderDetail.setOdaRemarkType(pAdjustModel.getAdjRemarkType());

            purPOrderDetailDao.insertSelective(pOrderDetail);
            PInventory pInventory = new PInventory();
            pInventory.setItyProCode(pAdjustModel.getAdjProCode());
            pInventory.setItyProName(pAdjustModel.getAdjProName());
            pInventory.setItyMatDesc(pAdjustModel.getAdjMatDesc());
            pInventory.setItySapCode(pAdjustModel.getAdjMatCode());
            pInventory.setItyNum(pAdjustModel.getAdjNum());
            pInventory.setItyAvaNum(pAdjustModel.getAdjNum());
            pInventory.setItyPrice(pAdjustModel.getAdjPrice());
            pInventory.setItyPosition(pAdjustModel.getAdjPosition());
            pInventory.setItyWhiId(pAdjustModel.getAdjWhiId());
            pInventory.setItyNumUnit(pAdjustModel.getAdjUnit());
            pInventory.setItyRemark(pAdjustModel.getAdjSpec());
            pInventory.setItyBookkeepingDate(pAdjustModel.getAdjBookkeepingDate());
            pInventory.setItyTrackCode(pAdjustModel.getAdjTrackCode());
            pInventory.setItyStockType(pAdjustModel.getAdjStockType());
            pInventory.setItyRowNo(pOrderDetail.getOdaId());
            pInventory.setItyAdjustFlag(1);
            pInventory.setItyRemarkType(pOrderDetail.getOdaRemarkType());
            purPInventoryDao.insertSelective(pInventory);
            PAdjustModel changePAdjustModel = new PAdjustModel();
            changePAdjustModel.setAdjId(pAdjustModel.getAdjId());
            changePAdjustModel.setAdjStatus(3);
            changePAdjustModel.setAdjOdrId(pOrder.getOdrId());
            changePAdjustModel.setAdjOdaId(pOrderDetail.getOdaId());
            changePAdjustModel.setAdjCheckerId(ShiroUtils.getUser().getId());
            changePAdjustModel.setAdjCheckerDatetime(new Date());
            purAdjustDao.updateSelectiveByPrimaryKey(changePAdjustModel);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("审核通过成功！");
            logService.saveLog("【外购件】审核库存调整", "审核库存调整的物料描述：" + pAdjustModel.getAdjMatDesc());
        }
        //当前状态是非新增
        else if (pAdjustModel.getAdjNewFlag().equals(0)) {
            //判断调整的库存是否被标记，如果被标记则无法审核通过
            PInventory pInventory = purPInventoryDao.findByPrimaryKey(pAdjustModel.getAdjItyId());
//            if (!pInventory.getItyNum().equals(pInventory.getItyAvaNum())) {
//                jsonResult.setType(JsonResult.ResultType.ERROR);
//                jsonResult.setMsg("因此库存被标记，所以无法审核通过！");
//                return jsonResult;
//            }
            if (!pAdjustModel.getAdjPrice().equals(pInventory.getItyPrice()) ||
                    !pAdjustModel.getAdjMatDesc().equals(pInventory.getItyMatDesc()) ||
                    !pAdjustModel.getAdjUnit().equals(pInventory.getItyNumUnit())) {
                POrderDetail pOrderDetail = new POrderDetail();
                pOrderDetail.setOdaOdrId(null);
                pOrderDetail.setOdaType(1);
                pOrderDetail.setOdaProCode(pInventory.getItyProCode());
                pOrderDetail.setOdaProName(pInventory.getItyProName());
                pOrderDetail.setOdaMatDesc(pInventory.getItyMatDesc());
                pOrderDetail.setOdaNum(pInventory.getItyNum());
                pOrderDetail.setOdaNumPrice(pInventory.getItyPrice());
                pOrderDetail.setOdaPosition(pInventory.getItyPosition());
                pOrderDetail.setOdaWhiId(pInventory.getItyWhiId());
                pOrderDetail.setOdaNumUnit(pInventory.getItyNumUnit());
                pOrderDetail.setOdaTotalPrice(pInventory.getItyAmount());
                pOrderDetail.setOdaSpec(pInventory.getItyRemark());
                pOrderDetail.setOdaBookkeepingDate(pInventory.getItyBookkeepingDate());
                pOrderDetail.setOdaSapCode(pInventory.getItySapCode());
                pOrderDetail.setOdaTrackCode(pInventory.getItyTrackCode());
                pOrderDetail.setOdaRedFlag(0);
                pOrderDetail.setOdaOutItyId(pInventory.getItyId());
                pOrderDetail.setOdaRemarkType(pInventory.getItyRemarkType());
                pOrderDetail.setContractNo(pInventory.getContractNo());
                pOrderDetail.setOdaCaseNo(pInventory.getItyCaseNo());
                pOrderDetail.setOdaWhiId(pInventory.getItyWhiId());
                pOrderDetail.setOdaOutWhiId(pInventory.getItyWhiId());
                pOrderDetail.setOdaCaseNo(pInventory.getItyCaseNo());
                pOrderDetail.setContractNo(pInventory.getContractNo());
                purPOrderDetailDao.insertSelective(pOrderDetail);
                PInventory changeNumPInventory = new PInventory();
                changeNumPInventory.setItyId(pInventory.getItyId());
                changeNumPInventory.setItyNum(0d);
                changeNumPInventory.setItyAvaNum(0d);
                purPInventoryDao.updateNumByPrimaryKey(changeNumPInventory);
                //再入库
                POrderDetail inPOrderDetail = purPOrderDetailDao.findOrderDetailByOdaId(pInventory.getItyRowNo());
                inPOrderDetail.setOdaOdrId(null);
                inPOrderDetail.setOdaNum(pAdjustModel.getAdjNum());
                inPOrderDetail.setOdaNumPrice(pAdjustModel.getAdjPrice());
                inPOrderDetail.setOdaTotalPrice(pAdjustModel.getAdjAmount());
                inPOrderDetail.setOdaNumUnit(pAdjustModel.getAdjUnit());
                inPOrderDetail.setOdaMatDesc(pAdjustModel.getAdjMatDesc());
                inPOrderDetail.setOdaRemarkType(pAdjustModel.getAdjRemarkType());
                purPOrderDetailDao.insertSelective(inPOrderDetail);
                PInventory inPInventory = new PInventory();
                inPInventory.setItyProCode(pAdjustModel.getAdjProCode());
                inPInventory.setItyProName(pAdjustModel.getAdjProName());
                inPInventory.setItyMatDesc(pAdjustModel.getAdjMatDesc());
                inPInventory.setItySapCode(pAdjustModel.getAdjMatCode());
                inPInventory.setItyNum(pAdjustModel.getAdjNum());
                inPInventory.setItyAvaNum(pAdjustModel.getAdjNum());
                inPInventory.setItyPrice(pAdjustModel.getAdjPrice());
                inPInventory.setItyPosition(pAdjustModel.getAdjPosition());
                inPInventory.setItyWhiId(pAdjustModel.getAdjWhiId());
                inPInventory.setItyNumUnit(pAdjustModel.getAdjUnit());
                inPInventory.setItyRemark(pAdjustModel.getAdjSpec());
                inPInventory.setItyBookkeepingDate(pAdjustModel.getAdjBookkeepingDate());
                inPInventory.setItyTrackCode(pAdjustModel.getAdjTrackCode());
                inPInventory.setItyStockType(pAdjustModel.getAdjStockType());
                inPInventory.setItyRowNo(inPOrderDetail.getOdaId());
                inPInventory.setItyAdjustFlag(1);
                inPInventory.setItyRemarkType(inPOrderDetail.getOdaRemarkType());
                inPInventory.setItyCaseNo(pOrderDetail.getOdaCaseNo());
                inPInventory.setContractNo(pOrderDetail.getOdaCaseNo());
                purPInventoryDao.insertSelective(inPInventory);
                PAdjustModel changePAdjustModel = new PAdjustModel();
                changePAdjustModel.setAdjId(pAdjustModel.getAdjId());
                changePAdjustModel.setAdjStatus(3);
                changePAdjustModel.setAdjOdaId(pOrderDetail.getOdaId());
                changePAdjustModel.setAdjCheckerId(ShiroUtils.getUser().getId());
                changePAdjustModel.setAdjCheckerDatetime(new Date());
                purAdjustDao.updateSelectiveByPrimaryKey(changePAdjustModel);
                if (pAdjustModel.getAdjPid() != null) {
                    changePAdjustModel.setAdjId(pAdjustModel.getAdjPid());
                    changePAdjustModel.setAdjStatus(3);
                    changePAdjustModel.setAdjOdaId(pOrderDetail.getOdaId());
                    changePAdjustModel.setAdjCheckerId(ShiroUtils.getUser().getId());
                    changePAdjustModel.setAdjCheckerDatetime(new Date());
                    purAdjustDao.updateSelectiveByPrimaryKey(changePAdjustModel);
                }
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
                jsonResult.setMsg("审核通过成功！");
                logService.saveLog("【外购件】审核库存调整", "审核库存调整的物料描述：" + pAdjustModel.getAdjMatDesc());
            } else if (!pAdjustModel.getAdjNum().equals(pInventory.getItyNum())) {
                POrderDetail pOrderDetail = new POrderDetail();
                pOrderDetail.setOdaOdrId(null);
                if (pAdjustModel.getAdjNum() > pInventory.getItyNum()) {
                    pOrderDetail.setOdaType(1);
                    pOrderDetail.setOdaNum(pAdjustModel.getAdjNum() - pInventory.getItyNum());
                } else {
                    pOrderDetail.setOdaType(2);
                    pOrderDetail.setOdaNum(pInventory.getItyNum());
                    pOrderDetail.setOdaNum(pInventory.getItyNum() - pAdjustModel.getAdjNum());
                }
                pOrderDetail.setOdaProCode(pInventory.getItyProCode());
                pOrderDetail.setOdaProName(pInventory.getItyProName());
                pOrderDetail.setOdaMatDesc(pInventory.getItyMatDesc());
                pOrderDetail.setOdaNumPrice(pInventory.getItyPrice());
                pOrderDetail.setOdaPosition(pInventory.getItyPosition());
                pOrderDetail.setOdaWhiId(pInventory.getItyWhiId());
                pOrderDetail.setOdaNumUnit(pInventory.getItyNumUnit());
                pOrderDetail.setOdaTotalPrice(pInventory.getItyAmount());
                pOrderDetail.setOdaSpec(pInventory.getItyRemark());
                pOrderDetail.setOdaBookkeepingDate(pInventory.getItyBookkeepingDate());
                pOrderDetail.setOdaSapCode(pInventory.getItySapCode());
                pOrderDetail.setOdaTrackCode(pInventory.getItyTrackCode());
                pOrderDetail.setOdaRedFlag(0);
                pOrderDetail.setOdaOutItyId(pInventory.getItyId());
                pOrderDetail.setOdaWhiId(pInventory.getItyWhiId());
                pOrderDetail.setOdaOutWhiId(pInventory.getItyWhiId());
                pOrderDetail.setOdaRemarkType(pInventory.getItyRemarkType());
                pOrderDetail.setOdaCaseNo(pInventory.getItyCaseNo());
                pOrderDetail.setOdaCaseNo(pInventory.getItyCaseNo());
                purPOrderDetailDao.insertSelective(pOrderDetail);
                PInventory changeNumPInventory = new PInventory();
                changeNumPInventory.setItyId(pInventory.getItyId());
                changeNumPInventory.setItyNum(pAdjustModel.getAdjNum());
                changeNumPInventory.setItyAvaNum(pAdjustModel.getAdjNum());
                changeNumPInventory.setItyAdjustFlag(1);
                purPInventoryDao.updateNumByPrimaryKey(changeNumPInventory);

                PAdjustModel changePAdjustModel = new PAdjustModel();
                changePAdjustModel.setAdjId(pAdjustModel.getAdjId());
                changePAdjustModel.setAdjStatus(3);
                changePAdjustModel.setAdjOdaId(pOrderDetail.getOdaId());
                changePAdjustModel.setAdjCheckerId(ShiroUtils.getUser().getId());
                changePAdjustModel.setAdjCheckerDatetime(new Date());
                purAdjustDao.updateSelectiveByPrimaryKey(changePAdjustModel);
                if (pAdjustModel.getAdjPid() != null) {
                    changePAdjustModel.setAdjId(pAdjustModel.getAdjPid());
                    changePAdjustModel.setAdjStatus(3);
                    changePAdjustModel.setAdjOdaId(pOrderDetail.getOdaId());
                    changePAdjustModel.setAdjCheckerId(ShiroUtils.getUser().getId());
                    changePAdjustModel.setAdjCheckerDatetime(new Date());
                    purAdjustDao.updateSelectiveByPrimaryKey(changePAdjustModel);
                }
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
                jsonResult.setMsg("审核通过成功！");
                logService.saveLog("【外购件】审核库存调整", "审核库存调整的物料描述：" + pAdjustModel.getAdjMatDesc());
            }
        }
        return jsonResult;
    }

    /**
     * @FunctionName: reviewAdopts
     * @Description: 批量审核通过
     * @DesignIdeas： 1.首先验证调整类别，如果是新增调整或盘点新增调整则直接生成库存数据和其相关的出入库明细数据。
     * 2.如果是调整申请和盘点调整，如果是盘点状态是盘亏，则先验证库存是否被标记（可用数量与在库数量书否相等），
     * 然后验证库存在库数量和可用数量是否足够调整，不足则提示。足够的话进行调整直接生成库存数据和其相关的出入
     * 库明细数据。
     * 3.如果是盘点状态是盘盈，则进行调整直接生成库存数据和其相关的出入库明细数据。
     * @Author: 徐一贺
     * @CreateDate: 2020/5/6 10:37
     * @Phone: 18241927380
     * @Param: [param] 主键
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult reviewAdopts(PAdjustModel param) {
        JsonResult jsonResult = new JsonResult();
        List<PAdjustModel> pAdjustModelList = purAdjustDao.findApplyListByAdjIds(param.getAdjIds());
        for (PAdjustModel pAdjustModel : pAdjustModelList) {
            if (pAdjustModel.getAdjNewFlag().equals(0)) {
                //判断调整的库存是否被标记，如果被标记则无法审核通过
                PInventory pInventory = purPInventoryDao.findByPrimaryKey(pAdjustModel.getAdjItyId());
                if (!pInventory.getItyNum().equals(pInventory.getItyAvaNum())) {
                    jsonResult.setType(JsonResult.ResultType.ERROR);
                    jsonResult.setMsg("物料描述为：【" + pInventory.getItyMatDesc() + "】的库存被标记，此次批量审核失败！");
                    return jsonResult;
                }
            }
        }
        for (PAdjustModel pAdjustModel : pAdjustModelList) {
            //当前状态是调整新增和盘点新增
            if (pAdjustModel.getAdjNewFlag().equals(1)) {
                POrder pOrder = new POrder();
                pOrder.setOdrCode(purWarInManageService.getOrderCode(PurchaseConstant.PRK));
                pOrder.setOdrType(1);
                pOrder.setOdrRedFlag(0);
                pOrder.setOdrChangeFlag(0);
                pOrder.setOdrReqFlag(0);
                pOrder.setOdrDate(new Date());
                pOrder.setOdrRemark("调整入库单");
                pOrder.setOdrMakeDate(new Date());
                pOrder.setOdrMakePersonId(ShiroUtils.getUser().getId());
                pOrder.setOdrAdjustFlag(1);
                purPOrderDao.insertSelective(pOrder);
                POrderDetail pOrderDetail = new POrderDetail();
                pOrderDetail.setOdaOdrId(pOrder.getOdrId());
                pOrderDetail.setOdaType(1);
                pOrderDetail.setOdaProCode(pAdjustModel.getAdjProCode());
                pOrderDetail.setOdaProName(pAdjustModel.getAdjProName());
                pOrderDetail.setOdaMatDesc(pAdjustModel.getAdjMatDesc());
                pOrderDetail.setOdaNum(pAdjustModel.getAdjNum());
                pOrderDetail.setOdaNumPrice(pAdjustModel.getAdjPrice());
                pOrderDetail.setOdaPosition(pAdjustModel.getAdjPosition());
                pOrderDetail.setOdaWhiId(pAdjustModel.getAdjWhiId());
                pOrderDetail.setOdaNumUnit(pAdjustModel.getAdjUnit());
                pOrderDetail.setOdaTotalPrice(pAdjustModel.getAdjAmount());
                pOrderDetail.setOdaSpec(pAdjustModel.getAdjSpec());
                pOrderDetail.setOdaBookkeepingDate(pAdjustModel.getAdjBookkeepingDate());
                pOrderDetail.setOdaSapCode(pAdjustModel.getAdjMatCode());
                pOrderDetail.setOdaTrackCode(pAdjustModel.getAdjTrackCode());
                pOrderDetail.setOdaRedFlag(0);
                purPOrderDetailDao.insertSelective(pOrderDetail);
                PInventory pInventory = new PInventory();
                pInventory.setItyProCode(pAdjustModel.getAdjProCode());
                pInventory.setItyProName(pAdjustModel.getAdjProName());
                pInventory.setItyMatDesc(pAdjustModel.getAdjMatDesc());
                pInventory.setItySapCode(pAdjustModel.getAdjMatCode());
                pInventory.setItyNum(pAdjustModel.getAdjNum());
                pInventory.setItyAvaNum(pAdjustModel.getAdjNum());
                pInventory.setItyPrice(pAdjustModel.getAdjPrice());
                pInventory.setItyPosition(pAdjustModel.getAdjPosition());
                pInventory.setItyWhiId(pAdjustModel.getAdjWhiId());
                pInventory.setItyNumUnit(pAdjustModel.getAdjUnit());
                pInventory.setItyRemark(pAdjustModel.getAdjSpec());
                pInventory.setItyBookkeepingDate(pAdjustModel.getAdjBookkeepingDate());
                pInventory.setItyTrackCode(pAdjustModel.getAdjTrackCode());
                pInventory.setItyStockType(pAdjustModel.getAdjStockType());
                pInventory.setItyRowNo(pOrderDetail.getOdaId());
                pInventory.setItyAdjustFlag(1);
                purPInventoryDao.insertSelective(pInventory);
                PAdjustModel changePAdjustModel = new PAdjustModel();
                changePAdjustModel.setAdjId(pAdjustModel.getAdjId());
                changePAdjustModel.setAdjStatus(3);
                changePAdjustModel.setAdjOdrId(pOrder.getOdrId());
                changePAdjustModel.setAdjOdaId(pOrderDetail.getOdaId());
                changePAdjustModel.setAdjCheckerId(ShiroUtils.getUser().getId());
                changePAdjustModel.setAdjCheckerDatetime(new Date());
                purAdjustDao.updateSelectiveByPrimaryKey(changePAdjustModel);
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
                jsonResult.setMsg("审核通过成功！");
                logService.saveLog("【外购件】审核库存调整", "审核库存调整的物料描述：" + pAdjustModel.getAdjMatDesc());
            }
            //当前状态是非新增
            else if (pAdjustModel.getAdjNewFlag().equals(0)) {
                //判断调整的库存是否被标记，如果被标记则无法审核通过
                PInventory pInventory = purPInventoryDao.findByPrimaryKey(pAdjustModel.getAdjItyId());
//                if (!pInventory.getItyNum().equals(pInventory.getItyAvaNum())) {
//                    jsonResult.setType(JsonResult.ResultType.ERROR);
//                    jsonResult.setMsg("因此库存被标记，所以无法审核通过！");
//                    return jsonResult;
//                } else
                if (!pAdjustModel.getAdjPrice().equals(pInventory.getItyPrice()) ||
                        !pAdjustModel.getAdjMatDesc().equals(pInventory.getItyMatDesc()) ||
                        !pAdjustModel.getAdjUnit().equals(pInventory.getItyNumUnit())) {
                    POrderDetail pOrderDetail = new POrderDetail();
                    pOrderDetail.setOdaOdrId(null);
                    pOrderDetail.setOdaType(1);
                    pOrderDetail.setOdaProCode(pInventory.getItyProCode());
                    pOrderDetail.setOdaProName(pInventory.getItyProName());
                    pOrderDetail.setOdaMatDesc(pInventory.getItyMatDesc());
                    pOrderDetail.setOdaNum(pInventory.getItyNum());
                    pOrderDetail.setOdaNumPrice(pInventory.getItyPrice());
                    pOrderDetail.setOdaPosition(pInventory.getItyPosition());
                    pOrderDetail.setOdaWhiId(pInventory.getItyWhiId());
                    pOrderDetail.setOdaNumUnit(pInventory.getItyNumUnit());
                    pOrderDetail.setOdaTotalPrice(pInventory.getItyAmount());
                    pOrderDetail.setOdaSpec(pInventory.getItyRemark());
                    pOrderDetail.setOdaBookkeepingDate(pInventory.getItyBookkeepingDate());
                    pOrderDetail.setOdaSapCode(pInventory.getItySapCode());
                    pOrderDetail.setOdaTrackCode(pInventory.getItyTrackCode());
                    pOrderDetail.setOdaRedFlag(0);
                    pOrderDetail.setOdaOutItyId(pInventory.getItyId());
                    pOrderDetail.setOdaOutWhiId(pInventory.getItyWhiId());
                    pOrderDetail.setOdaWhiId(pInventory.getItyWhiId());
                    pOrderDetail.setContractNo(pInventory.getContractNo());
                    pOrderDetail.setOdaCaseNo(pInventory.getItyCaseNo());
                    purPOrderDetailDao.insertSelective(pOrderDetail);
                    PInventory changeNumPInventory = new PInventory();
                    changeNumPInventory.setItyId(pInventory.getItyId());
                    changeNumPInventory.setItyNum(0d);
                    changeNumPInventory.setItyAvaNum(0d);
                    purPInventoryDao.updateNumByPrimaryKey(changeNumPInventory);
                    //再入库
                    POrderDetail inPOrderDetail = purPOrderDetailDao.findOrderDetailByOdaId(pInventory.getItyRowNo());
                    inPOrderDetail.setOdaOdrId(null);
                    inPOrderDetail.setOdaNum(pAdjustModel.getAdjNum());
                    inPOrderDetail.setOdaNumPrice(pAdjustModel.getAdjPrice());
                    inPOrderDetail.setOdaTotalPrice(pAdjustModel.getAdjAmount());
                    inPOrderDetail.setOdaNumUnit(pAdjustModel.getAdjUnit());
                    inPOrderDetail.setOdaMatDesc(pAdjustModel.getAdjMatDesc());
                    purPOrderDetailDao.insertSelective(inPOrderDetail);
                    PInventory inPInventory = new PInventory();
                    inPInventory.setItyProCode(pAdjustModel.getAdjProCode());
                    inPInventory.setItyProName(pAdjustModel.getAdjProName());
                    inPInventory.setItyMatDesc(pAdjustModel.getAdjMatDesc());
                    inPInventory.setItySapCode(pAdjustModel.getAdjMatCode());
                    inPInventory.setItyNum(pAdjustModel.getAdjNum());
                    inPInventory.setItyAvaNum(pAdjustModel.getAdjNum());
                    inPInventory.setItyPrice(pAdjustModel.getAdjPrice());
                    inPInventory.setItyPosition(pAdjustModel.getAdjPosition());
                    inPInventory.setItyWhiId(pAdjustModel.getAdjWhiId());
                    inPInventory.setItyNumUnit(pAdjustModel.getAdjUnit());
                    inPInventory.setItyRemark(pAdjustModel.getAdjSpec());
                    inPInventory.setItyBookkeepingDate(pAdjustModel.getAdjBookkeepingDate());
                    inPInventory.setItyTrackCode(pAdjustModel.getAdjTrackCode());
                    inPInventory.setItyStockType(pAdjustModel.getAdjStockType());
                    inPInventory.setItyRowNo(inPOrderDetail.getOdaId());
                    inPInventory.setItyAdjustFlag(1);
                    inPInventory.setContractNo(pOrderDetail.getContractNo());
                    inPInventory.setItyCaseNo(pOrderDetail.getOdaCaseNo());
                    purPInventoryDao.insertSelective(inPInventory);
                    PAdjustModel changePAdjustModel = new PAdjustModel();
                    changePAdjustModel.setAdjId(pAdjustModel.getAdjId());
                    changePAdjustModel.setAdjStatus(3);
                    changePAdjustModel.setAdjOdaId(pOrderDetail.getOdaId());
                    changePAdjustModel.setAdjCheckerId(ShiroUtils.getUser().getId());
                    changePAdjustModel.setAdjCheckerDatetime(new Date());
                    purAdjustDao.updateSelectiveByPrimaryKey(changePAdjustModel);
                    if (pAdjustModel.getAdjPid() != null) {
                        changePAdjustModel.setAdjId(pAdjustModel.getAdjPid());
                        changePAdjustModel.setAdjStatus(3);
                        changePAdjustModel.setAdjOdaId(pOrderDetail.getOdaId());
                        changePAdjustModel.setAdjCheckerId(ShiroUtils.getUser().getId());
                        changePAdjustModel.setAdjCheckerDatetime(new Date());
                        purAdjustDao.updateSelectiveByPrimaryKey(changePAdjustModel);
                    }
                    jsonResult.setType(JsonResult.ResultType.SUCCESS);
                    jsonResult.setMsg("审核通过成功！");
                    logService.saveLog("【外购件】审核库存调整", "审核库存调整的物料描述：" + pAdjustModel.getAdjMatDesc());
                } else if (!pAdjustModel.getAdjNum().equals(pInventory.getItyNum())) {
                    POrderDetail pOrderDetail = new POrderDetail();
                    pOrderDetail.setOdaOdrId(null);
                    if (pAdjustModel.getAdjNum() > pInventory.getItyNum()) {
                        pOrderDetail.setOdaType(1);
                        pOrderDetail.setOdaNum(pAdjustModel.getAdjNum() - pInventory.getItyNum());
                    } else {
                        pOrderDetail.setOdaType(2);
                        pOrderDetail.setOdaNum(pInventory.getItyNum());
                        pOrderDetail.setOdaNum(pInventory.getItyNum() - pAdjustModel.getAdjNum());
                    }
                    pOrderDetail.setOdaProCode(pInventory.getItyProCode());
                    pOrderDetail.setOdaProName(pInventory.getItyProName());
                    pOrderDetail.setOdaMatDesc(pInventory.getItyMatDesc());
                    pOrderDetail.setOdaNumPrice(pInventory.getItyPrice());
                    pOrderDetail.setOdaPosition(pInventory.getItyPosition());
                    pOrderDetail.setOdaWhiId(pInventory.getItyWhiId());
                    pOrderDetail.setOdaNumUnit(pInventory.getItyNumUnit());
                    pOrderDetail.setOdaTotalPrice(pInventory.getItyAmount());
                    pOrderDetail.setOdaSpec(pInventory.getItyRemark());
                    pOrderDetail.setOdaBookkeepingDate(pInventory.getItyBookkeepingDate());
                    pOrderDetail.setOdaSapCode(pInventory.getItySapCode());
                    pOrderDetail.setOdaTrackCode(pInventory.getItyTrackCode());
                    pOrderDetail.setOdaRedFlag(0);
                    pOrderDetail.setOdaOutItyId(pInventory.getItyId());
                    pOrderDetail.setOdaOutWhiId(pInventory.getItyWhiId());
                    pOrderDetail.setOdaWhiId(pInventory.getItyWhiId());
                    pOrderDetail.setContractNo(pInventory.getContractNo());
                    pOrderDetail.setOdaCaseNo(pInventory.getItyCaseNo());
                    purPOrderDetailDao.insertSelective(pOrderDetail);
                    PInventory changeNumPInventory = new PInventory();
                    changeNumPInventory.setItyId(pInventory.getItyId());
                    changeNumPInventory.setItyNum(pAdjustModel.getAdjNum());
                    changeNumPInventory.setItyAvaNum(pAdjustModel.getAdjNum());
                    changeNumPInventory.setItyAdjustFlag(1);
                    purPInventoryDao.updateNumByPrimaryKey(changeNumPInventory);

                    PAdjustModel changePAdjustModel = new PAdjustModel();
                    changePAdjustModel.setAdjId(pAdjustModel.getAdjId());
                    changePAdjustModel.setAdjStatus(3);
                    changePAdjustModel.setAdjOdaId(pOrderDetail.getOdaId());
                    changePAdjustModel.setAdjCheckerId(ShiroUtils.getUser().getId());
                    changePAdjustModel.setAdjCheckerDatetime(new Date());
                    purAdjustDao.updateSelectiveByPrimaryKey(changePAdjustModel);
                    if (pAdjustModel.getAdjPid() != null) {
                        changePAdjustModel.setAdjId(pAdjustModel.getAdjPid());
                        changePAdjustModel.setAdjStatus(3);
                        changePAdjustModel.setAdjOdaId(pOrderDetail.getOdaId());
                        changePAdjustModel.setAdjCheckerId(ShiroUtils.getUser().getId());
                        changePAdjustModel.setAdjCheckerDatetime(new Date());
                        purAdjustDao.updateSelectiveByPrimaryKey(changePAdjustModel);
                    }
                    jsonResult.setType(JsonResult.ResultType.SUCCESS);
                    jsonResult.setMsg("审核通过成功！");
                    logService.saveLog("【外购件】审核库存调整", "审核库存调整的物料描述：" + pAdjustModel.getAdjMatDesc());
                }
            }
        }
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        jsonResult.setMsg("批量审核通过成功！");
        return jsonResult;
    }

    /**
     * @FunctionName: reviewFail
     * @Description: 审核不通过
     * @Author: 徐一贺
     * @CreateDate: 2020/5/6 15:00
     * @Phone: 18241927380
     * @Param: [param] 主键
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult reviewFail(PAdjustModel param) {
        param.setAdjStatus(4);
        purAdjustDao.updateSelectiveByPrimaryKey(param);
        param.setAdjId(purAdjustDao.getPAdjustModelByPrimaryKey(param.getAdjId()).getAdjPid());
        purAdjustDao.updateSelectiveByPrimaryKey(param);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        jsonResult.setMsg("审核不通过成功！");
        return jsonResult;
    }
}


